package com.sangeng.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.constants.SystemConstants;
import com.sangeng.domain.dto.RoleDto;
import com.sangeng.domain.entity.Role;
import com.sangeng.domain.entity.RoleMenu;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.mapper.RoleMapper;
import com.sangeng.mapper.RoleMenuMapper;
import com.sangeng.service.RoleMenuService;
import com.sangeng.service.RoleService;
import com.sangeng.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2024-08-13 21:43:24
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMenuService roleMenuService;
    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //判断是否是管理员 如果是返回集合中只需要有admin
        if (id == 1L){
            List<String> roleKeys=new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
        //否则查询用户所具有的角色信息
        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult rolePageList(Long pageNum, Long pageSize, String roleName, String status) {
        //对角色名字进行模糊查询
        LambdaQueryWrapper<Role> roleLQW=new LambdaQueryWrapper<>();
        roleLQW.like(StringUtils.hasText(roleName),Role::getRoleName,roleName)
                .eq(StringUtils.hasText(status),Role::getStatus,status);
        //按照role_sort进行升序排列。
        roleLQW.orderByAsc(Role::getRoleSort);
        Page<Role> rolePage=new Page<>(pageNum,pageSize);
        page(rolePage,roleLQW);
        PageVo pageVo=new PageVo(rolePage.getRecords(),rolePage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult changeStatus(RoleDto roleDto) {
        //要求能够修改角色的停启用状态
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        updateById(role);
        return ResponseResult.okResult();
    }


    @Override
    @Transactional
    public ResponseResult addRile(RoleDto roleDto) {
        //存储新增用户
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        save(role);
        //存储角色相关联的菜单
        List<RoleMenu> roleMenus = roleDto
                .getMenuIds().stream()
                .map(menuId -> new RoleMenu(role.getId(), menuId)).collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenus);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getRoleById(Long id) {
        Role role = getById(id);
        RoleDto roleDto = BeanCopyUtils.copyBean(role, RoleDto.class);
        return ResponseResult.okResult(roleDto);
    }

    @Override
    @Transactional
    public ResponseResult updateRole(RoleDto roleDto) {
        //根据角色id删除关联的菜单权限
        LambdaQueryWrapper<RoleMenu>  roleMenuLQw=new LambdaQueryWrapper<>();
        roleMenuLQw.eq(RoleMenu::getRoleId,roleDto.getId());
        roleMenuService.remove(roleMenuLQw);
        //重新存储
        List<RoleMenu> roleMenus = roleDto.getMenuIds()
                .stream().map(m -> new RoleMenu(roleDto.getId(), m))
                .collect(Collectors.toList());
        //更新角色
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        updateById(role);
        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult deleteRole(Long id) {
        //删除相关联的菜单权限
        LambdaQueryWrapper<RoleMenu> roleMenuLQW=new LambdaQueryWrapper<>();
        roleMenuLQW.eq(RoleMenu::getRoleId,id);
        roleMenuService.remove(roleMenuLQW);
        removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listAllRole() {
        //查询的是所有状态正常的角色 0正常 1禁用
        LambdaQueryWrapper<Role> roleLQW=new LambdaQueryWrapper<>();
        roleLQW.eq(Role::getStatus, SystemConstants.NORMAL);
        List<Role> list = list(roleLQW);
        return ResponseResult.okResult(list);
    }
}
